{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am power and i love you and i am power\n"
     ]
    }
   ],
   "source": [
    "# format函数格式化字符串\n",
    "s=\"i am {0} and i love {1} and i am {0}\".format('power','you')\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 顺序分支和循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入的你的年龄20\n",
      "你的年龄是20\n",
      "有好东西可以看了\n"
     ]
    }
   ],
   "source": [
    "#if语句的操作\n",
    "age=input('请输入的你的年龄')\n",
    "print(\"你的年龄是{}\".format(age))\n",
    "age=int(age)\n",
    "if age<50 and age>18:\n",
    "    print('有好东西可以看了')\n",
    "elif age>50:\n",
    "    print('大爷，保重身体啊')\n",
    "else:\n",
    "    print('小毛孩，一边玩去')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for-else 循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1是奇数\n",
      "2是偶数\n",
      "3是奇数\n",
      "4是偶数\n",
      "5是奇数\n",
      "6是偶数\n",
      "7是奇数\n",
      "8是偶数\n",
      "9是奇数\n",
      "10是偶数\n",
      "循环结束\n"
     ]
    }
   ],
   "source": [
    "for index in range(1,11):    #randint(1,10）表示的是1-10的任意一个数\n",
    "    if index%2==0:\n",
    "        print('{}是偶数'.format(index))\n",
    "        continue                    #break是直接跳出循环，pass是用来占位的，无意义\n",
    "    else:\n",
    "        print('{}是奇数'.format(index))\n",
    "else:              #循环结束后执行的内容\n",
    "    print('循环结束')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'int' object is not iterable",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-24687a42d2d6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'int' object is not iterable"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "# import random\n",
    "for index in random.randint(1,10):\n",
    "    print(index)\n",
    "#这是一个错误的写法，因为randint(1,10)得到的是一个数而不是一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am power\n"
     ]
    }
   ],
   "source": [
    "var=\"power\"\n",
    "print(\"i am \"+var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while-else 循环的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1年取出来可以得到525000.0\n",
      "第2年取出来可以得到551250.0\n",
      "第3年取出来可以得到578812.5\n",
      "第4年取出来可以得到607753.125\n",
      "第5年取出来可以得到638140.78125\n",
      "第6年取出来可以得到670047.8203125\n",
      "第7年取出来可以得到703550.211328125\n",
      "第8年取出来可以得到738727.7218945313\n",
      "第9年取出来可以得到775664.1079892579\n",
      "第10年取出来可以得到814447.3133887209\n",
      "第11年取出来可以得到855169.6790581569\n",
      "第12年取出来可以得到897928.1630110648\n",
      "第13年取出来可以得到942824.571161618\n",
      "第14年取出来可以得到989965.799719699\n",
      "第15年取出来可以得到1039464.089705684\n",
      "第15年终于翻倍了\n"
     ]
    }
   ],
   "source": [
    "benjin=50_0000\n",
    "year=0\n",
    "while benjin<=100_0000:\n",
    "    benjin*=(1+0.05)\n",
    "    year+=1\n",
    "    print(\"第{0}年取出来可以得到{1}\".format(year,benjin))\n",
    "else:\n",
    "    print(\"第{}年终于翻倍了\".format(year))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
